
<div id="account_page_cover" style="height: 160px; background: ${session['user'].userInfo.pageCover};background-image: url(${session['user'].userInfo.pageCover})">
    <div class="upload-co">
        上传封面
        <input type="file" name="photo">
    </div>
</div>
<div id="account_center_avatar">
    <div class="avatar">
        <img src="${session['user'].avatar}">
        <div class="upload-co">
            上传头像
            <input type="file" name="photo">
        </div>
    </div>
</div>
<script>
    $(document).ready(function(){
        var send = function(file, callback){
            var formData = new FormData();
            formData.append("file", file);
            var xhr = new XMLHttpRequest();
            xhr.open('POST', '/attach/save');
            xhr.onreadystatechange = function(){
                if (xhr.readyState==4 && xhr.status==200){
                    callback(xhr)
                }
            };
            xhr.send(formData);
        }
        $("#account_center_avatar .avatar input").on("change", function(e){
            var file = e.target.files[0];
            var allows = ["image/gif", "image/jpg", "image/png"];
            console.log(file.type.trim() in allows);
            send(file, function(xhr){
                var response = JSON.parse(xhr.response);
                if(response.id){
                    $.post("/account/set_avatar", {
                        url: "/attach/dis/" + response.id
                    }, function(response){
                        Pinion.simpleNotify("更换头像成功");
                    });
                    $("#account_center_avatar .avatar img").attr({src: "/attach/dis/" + response.id});
                }
            })
        });

        $("#account_page_cover input").on("change", function(e){
            var file = e.target.files[0];
            var allows = ["image/gif", "image/jpg", "image/png"];
            console.log(file.type.trim() in allows);
            send(file, function(xhr){
                var response = JSON.parse(xhr.response);
                if(response.id){
                    $.post("/account/set_avatar", {
                        page_cover: "/attach/dis/" + response.id
                    }, function(response){
                        Pinion.simpleNotify("修改成功");
                    });
                    $("#account_page_cover").css({background: "url(/attach/dis/" + response.id + ")"});
                }
            })
        });
    });
</script>